import axios from 'axios';

// 1. 初始状态.
const state = () => ({
    brief: [
        {
            key: 'projectNumber',
            desc: '我的项目',
            number: 0,
            icon: require('@/assets/img/my-project@1x.png'),
            bgColor: '#F2F7FF',
            color: '#2A71FC',
        },
        {
            key: 'serviceNumber',
            desc: '我的服务',
            number: 0,
            icon: require('@/assets/img/my-instance@1x.png'),
            bgColor: '#F2FCFB',
            color: '#2DC4B9',
        },
        {
            key: 'instanceNumber',
            desc: '我的实例',
            number: 0,
            icon: require('@/assets/img/my-instance@1x.png'),
            bgColor: '#FEF9F0',
            color: '#2DC4B9',
        },
    ],
    detail: [
        {
            key: 'totalProjectNumber',
            desc: 'TOTAL PROJECTS',
            number: 0,
            icon: require('@/assets/img/detail-project@1x.png'),
            fromColor: '#1D64EF',
            toColor: '#36D1FF',
        },
        {
            key: 'totalServiceNumber',
            desc: 'TOTAL SERVICE',
            number: 0,
            icon: require('@/assets/img/detail-instance@1x.png'),
            fromColor: '#4CD18C',
            toColor: '#1F9DBA',
        },
        {
            key: 'totalInstanceNumber',
            desc: 'TOTAL INSTANCE',
            number: 0,
            icon: require('@/assets/img/detail-instance@1x.png'),
            fromColor: '#FAF46B',
            toColor: '#EF9A36',
        },
        {
            key: 'upInstanceNumber',
            desc: 'INSTANCE RUNNING',
            number: 0,
            icon: require('@/assets/img/detail-running@1x.png'),
            fromColor: '#664CD1',
            toColor: '#175FBF',
        },
        {
            key: 'error',
            desc: 'INSTANCE ERROR',
            number: 0,
            icon: require('@/assets/img/detail-error@1x.png'),
            fromColor: '#EA593F',
            toColor: '#FFB94A',
        },
        {
            key: 'downInstanceNumber',
            desc: 'INSTANCE DOWN',
            number: 0,
            icon: require('@/assets/img/detail-down@1x.png'),
            fromColor: '#D11414',
            toColor: '#FF974A',
        },
        {
            key: 'totalEvents',
            desc: 'TOTAL EVENTS',
            number: 0,
            icon: require('@/assets/img/detail-events@1x.png'),
            fromColor: '#6B2EDE',
            toColor: '#B236FF',
        },
    ],
});

// 2. 计算属性.
const getters = {};

// 3. 异步修改.
const actions = {
    loadData({ state }) {
        axios.all([
            axios.get('/overview/myOverview'),
            axios.get('/overview/instanceOverview'),
        ]).then(axios.spread((brief, detail) => {
            state.brief.forEach((v) => {
                v.number = brief.data[v.key] || 0;
            });
            state.detail.forEach((v) => {
                v.number = detail.data[v.key] || 0;
            });
        }));
    },
};

// 4. 同步修改.
const mutations = {
};

// 5. 模块导出.
export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations,
};
